import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { SetofkeyService } from "../../../../service/setofkey.service";
import { FormBuilder, Validators, FormControl } from "@angular/forms";
import { Command } from "../../../../shared/toolbar.service";
import { Observable } from "rxjs/Observable";
import { BladeData } from "../../../../shared/blade/blade-data";

import { FaultLevel } from "../../../../entity/fault-level.model";

import { SoundFile } from "../../../../entity/sound-file.model";
import { AlarmSoundService } from "../../../../service/alarm-sound.service";

@Component({
	selector: 'app-sound-edit',
	templateUrl: './sound-edit.component.html',
	styleUrls: ['./sound-edit.component.css']
})
export class SoundEditComponent extends BladeContainerBase<SoundFile> {

	public soundfile: File;
	public isUpload: boolean = true;

	constructor(
		private fb: FormBuilder,
		private alarmSoundService: AlarmSoundService) {
		super(fb)
	}

	ngOnInit() {
		//编辑时注册按钮和按钮命令    
		this.commands = [
			new Command("save", "上传", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
			new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
		]
		this.currentEntity = new SoundFile(0, '', true);
		super.ngOnInit();
	}

	exeSaveChange(blade: any): void {
		this.saveChange();
	}
	canSaveChange(blade: any): boolean {
		return this.hasChange() && !this.hasError() && this.soundfile != null;
	}
	exeReset(blade: any): void {
		return this.formGroup.reset(this.currentEntity);
	}
	canReset(blade: any): boolean {
		return this.hasChange();
	}

	/**
    * 创建表单
    * 
    * 
    * @memberof DicKeyEditComponent
    */
	createForm() {
		this.formGroup = this.fromBuider.group({
			"soundName": [this.currentEntity.soundName, [
				Validators.required,
				Validators.maxLength(20),
			]],
			//, (control: FormControl): any => this.soundNameAsnyValidator(control),名称的异步校验；
		});

		//上传按钮是否可用判断
		this.formGroup.controls["soundName"].valueChanges.subscribe(form => {
			if (this.formGroup.controls["soundName"].value != '') {
				this.isUpload = false;
			} else {
				this.isUpload = true;
			}
		})
	}

    /**
    * 设置表单错误对象
    * 
    * 
    * @memberof BladeContainerBase
    */
	setFormError(): void {
		this.formErrors = {
			'soundName': '',
		};
	}
    /**
    * 设置表单验证信息
    * 
    * 
    * @memberof BladeContainerBase
    */
	setValidationMessages(): void {
		this.validationMessages = {
			'soundName': {
				'required': '名称不能为空。',
				'maxlength': '名称不能多于20个字符长度。',
				'exist': '名称已存在',
			},
		};
	}

    /**
     * 异步校验Name是否存在
     * 
     * @param {FormControl} control 
     * @returns {Promise<true>} 
     * 
     * @memberof FarmEditComponent
     */
	// soundNameAsnyValidator(control: FormControl): any {
	// 	return this.alarmSoundService.hasSoundName(control.value, this.currentEntity.soundId)
	// 		.map(res => res ? { exist: true } : null);
	// }

	myUploader(event) {
		//上传成功的回调
		this.soundfile = event.files[0];
	}

	saveChange(): any | Promise<any> | Observable<any> {
		if (this.canSaveChange(this.currentBlade)) {
			this.bladeData.isLoading = true;
			let formValue = this.formGroup.value;
			this.currentEntity.soundName = formValue.soundName;
			return this.alarmSoundService.upload(this.soundfile)
				.then(res => {
					if (res) {
						this.currentEntity.content = res;
						this.alarmSoundService.create(this.currentEntity)
							.then(res => {
								(this.parentBlade.bladeContainerInstance).refresh(res);
								this.bladeService.closeBlade(this.currentBlade);
								return true;

							});

					}
				})
				.catch(error => this.bladeData.error = error)
				.then(res => this.bladeData.isLoading = false);
		}
	}
	onUpload(ev) {
		//this.formGroup.controls["content"].setValue(ev.files[0].name);
		this.soundfile = ev.files[0];

	}
	onRemove(ev) {
		//this.formGroup.controls["content"].setValue(this.currentEntity.content);
		this.soundfile = null;
	}

}



// WEBPACK FOOTER //
// ./src/app/module/basic/sound/sound-edit/sound-edit.component.ts